# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import time

def reinstall_pyqt():
    print("开始重新安装 PyQt6...")
    try:
        # 先卸载现有的 PyQt6
        print("卸载现有 PyQt6...")
        packages_to_remove = [
            'PyQt6',
            'PyQt6-Qt6',
            'PyQt6-sip',
            'PyQt6-WebEngine',
            'PyQt6-WebEngine-Qt6'
        ]
        
        for package in packages_to_remove:
            try:
                subprocess.check_call([
                    sys.executable,
                    '-m',
                    'pip',
                    'uninstall',
                    package,
                    '-y'
                ])
                time.sleep(1)
            except:
                pass
        
        # 安装最新稳定版本的 PyQt6 及其依赖
        print("安装 PyQt6...")
        subprocess.check_call([
            sys.executable,
            '-m',
            'pip',
            'install',
            'PyQt6==6.5.3',
            'PyQt6-Qt6==6.5.3',
            'PyQt6-sip==13.9.0',  # 更新到最新版本
            '--no-cache-dir',
            '--only-binary=:all:',
            '-i',
            'https://pypi.tuna.tsinghua.edu.cn/simple',
            '--trusted-host',
            'pypi.tuna.tsinghua.edu.cn'
        ])
        
        print("验证安装...")
        subprocess.check_call([
            sys.executable,
            '-c',
            'import PyQt6.QtWidgets; print("PyQt6 安装成功")'
        ])
        
        print("PyQt6 重新安装完成！")
        
    except Exception as e:
        print(f"安装过程中出现错误: {str(e)}")
        sys.exit(1)

if __name__ == '__main__':
    if hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
        print("检测到虚拟环境")
    else:
        print("警告: 未在虚拟环境中运行")
    
    reinstall_pyqt() 